import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:super_tool_server/business/config_manager.dart';
import 'package:super_tool_server/component/button/round_button.dart';
import 'package:super_tool_server/component/input_text/custom_text_field.dart';
import 'package:super_tool_server/component/theme/theme.dart';
import 'package:super_tool_server/model/config_info.dart';

class SettingWidget extends StatefulWidget {
  const SettingWidget({Key? key}) : super(key: key);

  @override
  State<SettingWidget> createState() => _SettingWidgetState();
}

class _SettingWidgetState extends State<SettingWidget> {
  late ConfigInfo configInfo;
  String configPath = '';
  String autotestPath = '';

  @override
  void initState() {
    super.initState();
    configInfo = ConfigManager.getInstance().getConfigInfo();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
      children: [
        Container(
          margin: const EdgeInsets.only(right: 30, bottom: 10),
          width: 125,
          height: 35,
          alignment: Alignment.centerLeft,
          child: SizedBox(
            width: 125,
            height: 45,
            child: RoundButton(
              text: '恢复默认配置',
              style: RoundButtonStyle.normal,
              shape: RoundButtonShape.round,
              size: RoundButtonSize.small,
              fontSize: 14,
              fontWeight: FontWeight.w900,
              textColor: Colors.white,
              borderRadius: 5,
              onPress: () async {
                configPath = '';
                await ConfigManager.getInstance().setConfigPath('');
                setState(() {});
              },
            ),
          ),
        ),
        Row(
          children: [
            const SelectableText(
              '配置文件路径',
              style: TextStyle(fontSize: 18),
            ),
            Container(
              margin: const EdgeInsets.only(right: 30, left: 30),
              // width: 125,
              height: 35,
              child: RoundButton(
                text: '选择文件',
                style: RoundButtonStyle.normal,
                shape: RoundButtonShape.round,
                size: RoundButtonSize.small,
                fontSize: 14,
                fontWeight: FontWeight.w900,
                textColor: Colors.white,
                borderRadius: 5,
                onPress: () async {
                  FilePickerResult? result = await FilePicker.platform.pickFiles(
                    dialogTitle: '请选择SuperTool工具的配置文件',
                    type: FileType.custom,
                    allowedExtensions: ['json'],
                  );
                  if (result != null && result.isSinglePick) {
                    String? tmp = result.files.single.path;
                    if (tmp != null && tmp.isNotEmpty) {
                      configPath = tmp;
                      await ConfigManager.getInstance().setConfigPath(configPath);
                      setState(() {});
                    }
                  }
                },
              ),
            ),
            SelectableText(
              ConfigManager.getInstance().getLocalConfigPath(),
              style: const TextStyle(fontSize: 18, color: Colors.purple),
            ),
            // Container(
            //   width: 500,
            //   height: 45,
            //   padding: const EdgeInsets.only(top: 2, bottom: 2, right: 2),
            //   margin: const EdgeInsets.only(top: 10),
            //   decoration: BoxDecoration(
            //     borderRadius: BorderRadius.all(
            //       Radius.circular(
            //         8,
            //       ),
            //     ),
            //     border: Border.all(
            //       color: CustomTheme.of(context)!.colors.primary,
            //       width: 2,
            //     ),
            //   ),
            //   child: CustomTextField(
            //     defaultText: ConfigManager.getInstance().getLocalConfigPath(),
            //     hintText: 'config path',
            //     maxLine: 1,
            //     maxLength: 300,
            //     border: Border.all(color: Colors.transparent, width: 0),
            //     customTextFieldShape: CustomTextFieldShape.round,
            //     customTextFieldMode: CustomTextFieldMode.large,
            //     customTextFieldSize: CustomTextFieldSize.normal,
            //     customTextFieldStyle: CustomTextFieldStyle.normal,
            //     isShowDeleteButton: true,
            //     onChanged: (text) {
            //       configPath = text.trim();
            //     },
            //   ),
            // ),
          ],
        ),
        const SizedBox(
          height: 16,
        ),
        SelectableText(
          '工具自身的日志存储目录 ${configInfo.localLogPath}',
          style: const TextStyle(fontSize: 18),
        ),
      ],
    );
  }
}
